package org.ruanjj.stack;

import java.util.*;

public class MinStack {
    Stack<Integer> minStack;
    Stack<Integer> orderedStack;
    public MinStack() {
        minStack = new Stack<>();
        orderedStack = new Stack<>();
    }

    public void push(int val) {
        minStack.add(val); //新增元素
        Stack<Integer> temp = new Stack<>();
        while(orderedStack.size() > 0){
            if(orderedStack.peek() < val){
                temp.add(orderedStack.pop());
            }else{
                orderedStack.add(val);
                break;
            }
        }

        while(temp.size() > 0){
            orderedStack.add(temp.pop());
        }
    }

    public void pop() {
        orderedStack.remove(minStack.peek());
        minStack.pop(); //删除元素
    }

    public int top() {
        return minStack.peek();
    }

    public int getMin() {
        return orderedStack.peek();
    }
}
